package de.hdu.pms.ho.ctrl;

import org.apache.log4j.Logger;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.hdu.pms.ho.dao.KategorieDao;
import de.hdu.pms.ho.model.Kategorie;

/**
 * Controller for editing, saving, deleting and showing all objects of the class {@link Kategorie}
 * 
 * @author Thomas Niedermeier
 *
 */
@Controller
public class KategorieController {
	
	private static Logger log = Logger.getLogger(KategorieController.class);
	private KategorieDao kategorieDao = null;

	public void setKategorieDao(KategorieDao kategorieDao) {
		this.kategorieDao = kategorieDao;
	}
	

	
	/**
	 * Creates a new {@link Kategorie} or updates an existing one
	 * @param id optional, only for update
	 * @return
	 */
	@RequestMapping(value="/EditKategorie.html", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam(required=false) Integer id) {
		log.debug("EditKateorie.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject("title", "Kategorie");
			mv.addObject("message", "Kategorie anlegen");
			mv.addObject(new Kategorie());	
		} else {
			mv.addObject("title", "Kategorie");
			mv.addObject("message", "Kategorie ändern");
			mv.addObject(kategorieDao.findById(id));
		}
		mv.addObject("edit", true);
		mv.setViewName("protected/edit-kategorie");
		return mv;
	}
	
	/**
	 * Saves an object with the type of {@link Kategorie}
	 * @param kategorie
	 * @param model
	 * @return redirects to the list of all existing kategories
	 */
	@RequestMapping(value="/SaveKategorie.html", method=RequestMethod.POST)
	public String save(Kategorie kategorie, Model model) {
		kategorieDao.save(kategorie);
		model.addAttribute("statusMessageKey", "person.form.msg.success");
		return "redirect:AlleKategorien.html";
	}
	
	/**
	 * Deletes an object {@link Kategorie}
	 * @param kategorie which should be deleted
	 * @return redirects to the list of all existing kategories
	 */
	@RequestMapping(value="/LoescheKategorie.html", method=RequestMethod.GET)
	public ModelAndView delete(Kategorie kategorie) {
		try {
			kategorieDao.delete(kategorie);
			return new ModelAndView("redirect:AlleKategorien.html");
		} catch (DataIntegrityViolationException e) {
			ModelAndView mv = new ModelAndView("protected/errordelete");
			mv.addObject("message", e.getMessage());
			return mv;
		}
	}
	
	/**
	 * Returns all existing objects with the type {@link Kategorie} from the database
	 * @return
	 */
	@RequestMapping(value="/AlleKategorien.html", method=RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Kategorie");
		mv.addObject("message", "Alle Kategorien des Hotels");
		mv.addObject("kategorien", kategorieDao.findAll());
		mv.addObject("edit", true);
		mv.setViewName("protected/list-kategorie");
		return mv;
	}
	
	
}
